package com.example.demo.base.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by fed on 2015/11/19.
 *
 * @author yingzhi zhu
 * 对请求头的设置 跨域设置
 */
@Component
public class SimpleCORSFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type,Access-Token");
        response.setHeader("Access-Control-Expose-Headers", "*");
        System.out.println("requestURL "+((HttpServletRequest)req).getRequestURI());
        chain.doFilter(req, res);
    }

    /**
     * {
     * "response_time": 0,
     * "meta": {
     * "code": 401,
     * "message": "用户身份已过期 请重新登陆!",
     * "type": "AuthError"
     * }
     * }
     */
    @Override
    public void destroy() {

    }
}
